<?php


namespace app\webs\controller;


use app\common\controller\Api;
use app\webs\model\HyqModel;
use app\webs\validate\HyqValidate;

class Hyq extends Api
{
    public function index()
    {
        $index = HyqModel::order('id','DESC')
            ->where('switch',1)
            ->field('id,members_id,name,image,text,images,num,create_time')
            ->select();
        if (!$index){
            $this->error('查询失败');
        }
        $this->success('查询成功',$index);
    }

    public function add()
    {
        $data = input('post.');
        //验证数据
        $validate = new HyqValidate();
        $result = $validate->scene('add')->check($data);
        if (!$result){
            $this->error($validate->getError());
        }
        $hyq = HyqModel::where('members_id',$data['members_id'])
            ->where('text',$data['text'])
            ->where('images',$data['images'])
            ->find();
        if ($hyq){
            $this->error('该动态已发布');
        }
        $data['create_time'] = date('Y-m-d H:i:s');
        if (HyqModel::create($data)){
            $this->success('添加成功');
        }
        $this->error('添加失败');
    }

    public function dz()
    {
        $id = input('id/d');
        $index = HyqModel::find($id);

        $index->num = round($index['num'] + 1);
        if ($index->save()) {
            $this->success('修改成功');
        }
        $this->error('修改失败');
    }

    public function delete()
    {
        $id = input('id/d');
        if (empty($id)) {
            $this->error('id不能为空');
        }
        $news = HyqModel::find($id);
        if (!$news) {
            $this->error('地址不存在');
        }

        if ($news->delete()) {
            $this->success('删除成功');
        } else {
            $this->error('删除失败');
        }
    }


}